package com.ewzj.bigdata.mvp.presenter

import android.app.Application
import android.support.v7.app.AppCompatActivity
import com.bumptech.glide.Glide
import com.ewzj.bigdata.app.config.AppKey
import com.ewzj.bigdata.app.utils.FilesUtlis
import com.ewzj.bigdata.app.utils.RxUtils
import com.ewzj.bigdata.mvp.contract.CitySelectorActivityContract
import com.ewzj.bigdata.mvp.contract.SettingActivityContract
import com.ewzj.bigdata.mvp.model.bean.CitygroupBean
import com.ewzj.bigdata.mvp.model.reqbean.ReqCityBean
import com.ewzj.bigdata.mvp.model.reqbean.ReqSearchCityBean
import com.ewzj.bigdata.mvp.model.rspbean.BaseData
import com.ewzj.bigdata.mvp.model.rspbean.CityBean
import com.ewzj.bigdata.mvp.model.rspbean.CitySearchBean
import com.ewzj.bigdata.mvp.ui.adapter.CityAdapter
import com.ewzj.bigdata.mvp.ui.adapter.HotcityAdapter
import com.google.gson.Gson
import com.jess.arms.mvp.BasePresenter
import com.jess.arms.utils.DataHelper
import com.jess.arms.utils.UiUtils
import io.reactivex.Observable
import io.reactivex.ObservableEmitter
import io.reactivex.ObservableOnSubscribe
import io.reactivex.android.schedulers.AndroidSchedulers
import io.reactivex.observables.GroupedObservable
import io.reactivex.schedulers.Schedulers
import io.rx_cache2.internal.RxCache
import me.jessyan.rxerrorhandler.core.RxErrorHandler
import me.jessyan.rxerrorhandler.handler.ErrorHandleSubscriber
import java.io.File
import java.util.*
import javax.inject.Inject

/**
 * author:zhangjing
 * 作用:
 * return:
 */
class SettingActivityPresenter @Inject constructor(val mGson: Gson, val mErrorHandler: RxErrorHandler, val application: Application, view: SettingActivityContract.View, model: SettingActivityContract.Model) : BasePresenter<SettingActivityContract.Model, SettingActivityContract.View>(model, view) {

   fun getCacacheFileSize(){
        var cache=mModel.getCacheFileSize()
        mRootView.setCacheSize(cache)
    }

    fun deleteCache(){
        Observable.create(object :ObservableOnSubscribe<String>{
            override fun subscribe(e: ObservableEmitter<String>) {
                var file=DataHelper.getCacheFile(application)
                FilesUtlis.deleteDirectoryAllFile(File(file, "RxCache"))
                Glide.get(application).clearDiskCache()
                e.onNext("删除成功")
            }

        }).subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(object :ErrorHandleSubscriber<String>(mErrorHandler){
                    override fun onNext(t: String?) {
                        mRootView.showMessage(t)
                        mRootView.setCacheSize("0kb")
                    }

                })

    }
}